package day14;

import day13.User;

import java.io.*;
import java.security.PublicKey;
import java.sql.SQLOutput;
import java.text.DecimalFormat;
import java.util.*;

public class User1 implements Serializable{
    private String phone;
    private String name;
    private String password;
    private Package pack;
    private double consumerMoney;//消费金额
    private double balance;//余额
    private int realTall;
    private int realCount;
    private int realFlow;
    public void print(){
        DecimalFormat df=new DecimalFormat("#.##");
        System.out.println("注册成功,您的手机号:"+phone+",姓名"+name);
        System.out.println("消费金额:"+ consumerMoney+",当前余额"+df.format(balance));
        pack.print();
    }
    public User1(String phone, String name, String password, Package pack,double consumerMoney,
                 double balance) {
        this.phone = phone;
        this.name = name;
        this.password = password;
        this.pack = pack;
        this.consumerMoney=consumerMoney;
        this.balance = balance;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Package getPack() {
        return pack;
    }

    public void setPack(Package pack) {
        this.pack = pack;
    }

    public double getConsumerMoney() {
        return consumerMoney;
    }

    public void setConsumerMoney(double consumerMoney) {
        this.consumerMoney = consumerMoney;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public int getRealTall() {
        return realTall;
    }

    public void setRealTall(int realTall) {
        this.realTall = realTall;
    }

    public int getRealCount() {
        return realCount;
    }

    public void setRealCount(int realCount) {
        this.realCount = realCount;
    }

    public int getRealFlow() {
        return realFlow;
    }

    public void setRealFlow(int realFlow) {
        this.realFlow = realFlow;
    }
}
interface CallService{
    void call();
}
interface SendService{
    void send();
}
interface NexService{
    void net();
}
abstract class Package implements Serializable{//套餐
    double price;
    public abstract void print();
}
class  TallPackage extends Package{//话痨
    public TallPackage(double price){
        super.price=price;
    }
    public void print() {
        System.out.println("我是话痨套餐,价格:"+price);
    }
}
class SuperPackage extends Package {//超人
    public SuperPackage(double price){
        super.price=price;
    }
    public void print() {
        System.out.println("我是超人套餐,价格:"+price);
    }
}
class NetPackage extends Package {//网虫
    public NetPackage(double price){
        super.price=price;
    }
    public void print() {
        System.out.println("我是网虫套餐,价格:"+price);
    }
}
class TestUser1{
    //定义保存用户信息的集合
    static Map<String,User1> users=new HashMap<>();
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) {
        one();
    }
    public static void one(){
        while (true){
            System.out.println("欢迎来到用户系统");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("请输入编号");
            int n=sc.nextInt();
            switch (n){
                case 1:login();break;
                case 2:register();break;
                case 3:return;
            }
        }
    }
    public static void login(){
        System.out.println("登录功能:先遍历下前面加载好的map集合");
        for(String key:users.keySet()){
            User1 value=users.get(key);
            value.print();
        }

    }
    public static void register(){
        String[] phones=randPhone();
        System.out.println("请选择您心仪的号码");
        for(int i=0;i< phones.length;i++){//生成1-9序号
            System.out.print(i+1+" "+phones[i]+" ");
            if((i+1)%3==0) System.out.println();
        }
        System.out.println("请输入编号1-9:");
        int n=sc.nextInt();
        String phone=phones[n-1];
        System.out.println("请输入您心仪的套餐");
        System.out.println("1.话痨套餐 2.超人套餐 3.网虫套餐");
        int m=sc.nextInt();
        Package p=null;
        switch (m){
            case 1:p=new TallPackage(58); break;
            case 2:p=new SuperPackage(68); break;
            case 3:p=new NetPackage(38);
        }
        Package pack=p;
        System.out.println("请输入姓名:");
        String name=sc.next();
        System.out.println("请输入密码:");
        String password=sc.next();
        System.out.println("请输入预存的话费:");
        double money=0;
        while (true){
            money=sc.nextDouble();
            if(money<p.price){
                System.out.println("穷鬼,钱不够哇！快去充");
            }else {
                break;
            }
        }
        User1 user1=new User1(phone,name,password,p,
                p.price,money-p.price);
        //System.out.println(user1);
        user1.print();
        users.put(phone,user1);
        output();//序列化
    }
    public static void input(){//反序列化
            try(FileInputStream fis=new FileInputStream("d://ioi//map.xxx");
                ObjectInputStream ois=new ObjectInputStream(fis)){
                users=(Map<String,User1>)ois.readObject();
                System.out.println("用户信息获取成功");
                //input();
            }catch (FileNotFoundException e){
                System.out.println("第一次使用");

            }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void output(){//序列化
        File f=new File("d://ioi//map.xxx");
        if(!f.getParentFile().exists()){
            f.getParentFile().mkdirs();
        }
       try(FileOutputStream fos=new FileOutputStream(f);
           ObjectOutputStream oos=new ObjectOutputStream(fos)){
           oos.writeObject(users);
           System.out.println("用户信息保存成功");
       }catch (Exception e){
           e.printStackTrace();
       }
    }
    public static String[] randPhone(){
        Random r=new Random();
        String[] phones=new String[9];//用来存储9个生成的电话号码
//        Set<String> s=new HashSet<>();
//        phones=s.toArray(new String[0]);
        for(int i=0;i<phones.length;i++){//遍历phones数组，为每个元素生成一个电话号码
            StringBuilder sb=new StringBuilder("139");
            for(int j=0;j<8;j++){//控制每个号码的生成
                sb.append(r.nextInt(10));//0-9
            }
            phones[i]=sb.toString();
        }
        return phones;
    }
}